package labox.innovation.gameserver.network.serverpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.cache.HtmCache;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

/**
 * 
 * the HTML parser in the client knowns these standard and non-standard tags and attributes VOLUMN UNKNOWN UL U TT TR TITLE TEXTCODE TEXTAREA TD TABLE SUP SUB STRIKE SPIN SELECT RIGHT PRE P OPTION OL MULTIEDIT LI LEFT INPUT IMG I HTML H7 H6 H5 H4 H3 H2 H1 FONT EXTEND EDIT COMMENT COMBOBOX CENTER BUTTON BR BR1 BODY BAR ADDRESS A SEL LIST VAR FORE READONL ROWS VALIGN FIXWIDTH BORDERCOLORLI BORDERCOLORDA BORDERCOLOR BORDER BGCOLOR BACKGROUND ALIGN VALU READONLY MULTIPLE SELECTED TYP TYPE MAXLENGTH CHECKED SRC Y X QUERYDELAY NOSCROLLBAR IMGSRC B FG SIZE FACE COLOR DEFFON DEFFIXEDFONT WIDTH VALUE TOOLTIP NAME MIN MAX HEIGHT DISABLED ALIGN MSG LINK HREF ACTION
 * 
 * ���Ϳͻ��˽������������
 * 
 * @version $Revision: 1.3.2.1.2.3 $ $Date: 2005/03/27 15:29:57 $
 */
public final class NpcHtmlMessage extends L2GameServerPacket {
	// d S
	// d is usually 0, S is the html text starting with <html> and ending with </html>
	//
	private static final String _S__1B_NPCHTMLMESSAGE = "[S] 19 NpcHtmlMessage";

	private static final Logger _log = LoggerFactory.getLogger(NpcHtmlMessage.class.getName());

	private long _npcObjId;

	private String _html;

	private int _itemId;

	private boolean _validate = true;

	/**
	 * 
	 * @param npcObjId
	 * @param text
	 * @param itemId
	 */
	public NpcHtmlMessage(long npcObjId, int itemId) {
		_npcObjId = npcObjId;
		_itemId = itemId;
	}

	/**
	 * @param _characters
	 */
	public NpcHtmlMessage(long npcObjId, String text) {
		_npcObjId = npcObjId;
		setHtml(text);
	}

	public NpcHtmlMessage(long npcObjId) {
		_npcObjId = npcObjId;
	}

	/**
	 * disable building bypass validation cache for this packet
	 */
	public void disableValidation() {
		_validate = false;
	}

	@Override
	public void runImpl() {
		if (Config.BYPASS_VALIDATION && _validate)
			buildBypassCache(getClient().getActiveChar());
	}

	public void setHtml(String text) {
		if (text.length() > 8192) {
			_log.warn("Html is too long! this will crash the client!", Config.SERVER_ID, "NpcHtmlMessage");
			_html = "<html><body>Html was too long</body></html>";
			return;
		}
		_html = text; // html code must not exceed 8192 bytes
	}

	public String getHtml() {
		return this._html;
	}

	public boolean setFile(String path) {
		String content = HtmCache.getInstance().getHtm(path);

		if (content == null) {
			setHtml("<html><body>My Text is missing:<br>" + path + "</body></html>");
			_log.warn("missing html page " + path, Config.SERVER_ID, "NpcHtmlMessage");
			return false;
		}

		setHtml(content);
		return true;
	}

	public void replace(String pattern, String value) {
		_html = _html.replaceAll(pattern, value);
	}

	private final void buildBypassCache(FuzePcInstance activeChar) {
		if (activeChar == null)
			return;

		//		activeChar.clearBypass();
		//		int len = _html.length();
		//		for (int i = 0; i < len; i++) {
		//			int start = _html.indexOf("\"bypass ", i);
		//			int finish = _html.indexOf("\"", start + 1);
		//			if (start < 0 || finish < 0)
		//				break;
		//
		//			if (_html.substring(start + 8, start + 10).equals("-h"))
		//				start += 11;
		//			else
		//				start += 8;
		//
		//			i = finish;
		//			int finish2 = _html.indexOf("$", start);
		//			if (finish2 < finish && finish2 > 0)
		//				activeChar.addBypass2(_html.substring(start, finish2).trim());
		//			else
		//				activeChar.addBypass(_html.substring(start, finish).trim());
		//		}
	}

	@Override
	protected final void writeImpl() {
		writeByte(PTL_NPCHTMLMESSAGE);
		writeLong(_npcObjId);
		writeString(_html);
		writeInt(_itemId);
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.serverpackets.ServerBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _S__1B_NPCHTMLMESSAGE;
	}

}
